<link rel="stylesheet" href="assets/lib/eleTree/eleTree.css"/>

<form id="role-form" lay-filter="role-form" class="layui-form layui-form-pane" style="padding: 15px 15px 0px 15px;height: 100%">
	<div style="height: 87%;">
		<div class="eleTree ele1" style="height: 100%;"></div>
	</div>
	<div class="layui-form-item model-form-footer">
	    <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog"><i class="layui-icon">&#x1006;</i> 取消</button>
	    <!-- <button class="layui-btn layui-btn-primary" type="button" id="getChecked"> 获取选中节点</button> -->
	    <button class="layui-btn" lay-filter="role-form-submit" lay-submit><i class="layui-icon">&#xe605;</i> 保存</button>
	</div>
</form>

<script th:inline="javascript">
	/*<![CDATA[*/
		layui.use(['jquery','table','eleTree','sadmin', 'form'], function(){
		    var $ = layui.jquery;
		    var eleTree = layui.eleTree;
		    var table = layui.table;
	        var sadmin = layui.sadmin;
	        var form = layui.form;
		
			var el = eleTree.render({
				elem : '.ele1',
				data : [[${treeAuths}]],
				defaultExpandedKeys: [0],
				showCheckbox : true,
				renderAfterExpand: true,
				defaultExpandAll: true
			});
			
			el.setChecked([[${roleAuthPids}]]);
			
			var url = 'system/role/saveperms';
			// 表单提交事件
	        form.on('submit(role-form-submit)', function (data) {
	            var checkData = el.getChecked(false, true);
				if (checkData.length >0) {
					layer.load(2);
					var roleid = sadmin.getTempData('roleid');
					var authidArr = [];
					for (var i=0; i<checkData.length; i++) {
						authidArr.push(checkData[i].key);
					}
					console.log(authidArr.join(","));
					
					var data = {roleid: roleid, authids: authidArr.join(",")};
		            $.post(url, data, function (result) {
		                layer.closeAll('loading');
		                layer.msg(result.msg);
		                sadmin.finishPopupCenter();
		            });
				}
				else {
					layer.msg('请选择权限后保存');
				}
	            
	            return false;
	        });
		});
		
	/*]]>*/
</script>
